package cn.ac.sec.liquid.index;

import java.util.List;

import javax.servlet.http.HttpSession;

import cn.ac.sec.liquid.common.model.Oilfield;
import cn.ac.sec.liquid.common.model.User;

import com.jfinal.core.Controller;

/**
 * IndexController
 */
public class IndexController extends Controller {
	
	/**
	 * 注销或登录
	 * session.removeAttribute("login_user"); // 注销操作
	 */
	public void index() {
		HttpSession session = getSession(); // 获取session对象
		session.invalidate(); //使session失效，清空session，不能再使用之前的连接
		render("/login/login.html"); // 不指定绝对路径时，是指/index/index.html
	}
	
	/**
	 * 首页
	 */
	public void homePage() {
		render("/index/index.html");
	}
	
	/**
	 * 登录验证，默认是指/index.html
	 */
	public void login() {
		
		HttpSession session = getSession(); //获取session对象
		
		User mUser = getModel(User.class); //获取表单数据，创建对象后使用set为对象设定属性,此处只有username和passwd两个有值
		List<User> list = User.dao.find("select * from user where username=?", mUser.getUsername());
		if (list.size() > 0) { //即用户名有效
			
			if (mUser.getPasswd().equals(User.dao.find("select passwd from user where username=?", mUser.getUsername()).get(0).getPasswd())) {
				System.out.println("mUser.toString: " + mUser.toString() + mUser.getUId()); //{passwd:111, username:zzy}null
				session.setAttribute("login_user", mUser); //将当前登录用户放入session
				render("/index/index.html"); //不指定绝对路径时，是指/index/index.html
			} else {
				render("/login/login.html"); 
			}
		} else {
			render("/login/login.html"); 
		}
	}
	
	/**
	 * 显示日历
	 */
	public void calendar() {
		render("/index/calendar.html"); 
	}
	
	public void sidebar(){
		List<Oilfield> list = Oilfield.dao.find("select * from oilfield order by id");
		setAttr("list",list);
		renderJson();
	}
}